<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controllers;

use yii\web\Controller;
use app\models\GkyQyhSpAuthorization;
use app\api\Response;
use app\api\ApprovalMessage;
/**
 * Description of AuthorizationController
 *
 * @author yuilin
 */
class AuthorizationController extends Controller {

   function actionCreate($userId) {
        $params = \Yii::$app->request->post();
        $model = new GkyQyhSpAuthorization();
        $model->proposer_id = $userId;
        $model->approver_id = isset($params['approver_id']) ? $params['approver_id'] : 1;
        $model->reason = isset($params['reason']) ? $params['reason'] : '';
        $model->time = isset($params['time']) ? $params['time'] : '0000-00-00 00:00:00';
        $model->create_time = date('Y-m-d H:i:s', time());
        $model->print_code = 'AUTH' . rand(1000, 9999) . time();
        $model->authorize_person=isset($params['authorize_person'])?$params['authorize_person']:'';
        $model->authorize_dept=isset($params['department'])?$params['department']:'';
        $model->status = 0;
        $model->job=isset($params['job'])?$params['job']:'';
        if ($model->save()) {
            Response::setHeader();
            echo 100;
            ApprovalMessage::sendMessage($model->proposer_id, $model->approver_id, 0, '授权委托');
        } else {
            Response::setHeader();
            echo -100;
        }
    }
    
    
    public function actionGetAuthorization($printCode) {
        $result = GkyQyhSpAuthorization::getAuthorization($printCode);
        Response::setHeader();
        echo json_encode($result);
    }
    
    
    public function actionAgree($printCode,$opinion) {
        if (GkyQyhSpAuthorization::agreeAuthorization($printCode,$opinion)) {
            Response::setHeader();
            echo 1;
            $model = GkyQyhSpAuthorization::findOne(['print_code' => $printCode]);

            if (\Yii::$app->request->isGet) {
                ApprovalMessage::sendMessage($model->approver_id, $model->proposer_id, 1, '授权委托', $printCode);
            }
        } else {
            Response::setHeader();
            echo -1;
        }
    }

    public function actionRefuse($printCode,$opinion) {
        if (GkyQyhSpAuthorization::refuseAuthorization($printCode,$opinion)) {
            Response::setHeader();
            $model = GkyQyhSpAuthorization::findOne(['print_code' => $printCode]);
            if (\Yii::$app->request->isGet) {
                ApprovalMessage::sendMessage($model->approver_id, $model->proposer_id, -1, '授权委托', $printCode);
            }
            echo 1;
        } else {
            Response::setHeader();
            echo -1;
        }
    }
    

}
